/**
 * Created by huachuan on 2016/1/7.
 */
angular.module('msgboxservice',[])
  //消息盒子Service
  .factory('MsgBox', function($resource,$rootScope,ENV){
    //访问的地址
    var APIUrl = ENV.api;
    var msgBoxCountJson,msgBoxListJson,updateMsgStateJson,deleteMsgBoxJson;
    //获取$resource
    var msgBoxResource = $resource(APIUrl+'findMessage/getMessageList.do');
    var getMsgBoxCountResource = $resource(APIUrl+'findMessage/getMessageCount.do');
    var updatemsgBoxResource = $resource(APIUrl+'findMessage/updateMsgBoxStatus.do');
    var deletemsgBoxResource = $resource(APIUrl+'findMessage/deleteMsgBox.do');
    var readMsgBoxResource = $resource(APIUrl+'findMessage/readMsgBox.do');
    return{
      //将消息盒子标记为已读
      readMsgBox:function(msgBoxIds){
        readMsgBoxResource.save({
          msgBoxIds:msgBoxIds
        },function(response){
          $rootScope.$broadcast('MsgBox.readMsgBoxUpdated');
        });
      },
      deleteMsgBox:function(msgBoxIds){
        deletemsgBoxResource.save({
          msgBoxIds:msgBoxIds
        },function(response){
          deleteMsgBoxJson = response;
          $rootScope.$broadcast('MsgBox.deleteMsgBoxUpdated');
        });
      },
      getDeleteMsgBoxJson:function(){
        return deleteMsgBoxJson;

      },
      updateMsgState:function(mnId){
        updatemsgBoxResource.save({
          mn_id:mnId
        },function(response){
          updateMsgStateJson = response;
          $rootScope.$broadcast('MsgBox.updateMsgStateUpdated');
        });
      },
      getUpdateStateJson:function(){
        return updateMsgStateJson;
      },
      //获取消息盒子的数量
      getMsgBoxCount:function(memberId){
        getMsgBoxCountResource.save({
          member_id:memberId
        },function(response){
          msgBoxCountJson = response;
          $rootScope.$broadcast('MsgBox.msgBoxCountUpdated');
        });
      },
      //获取消息盒子的数量
      getMsgBoxCountJson:function(){
        return msgBoxCountJson;
      },
      //获取消息盒子的信息
      getMsgBoxList:function(memberId){
        msgBoxResource.save({
          member_id:memberId
        },function(response){
          msgBoxListJson = response;
          $rootScope.$broadcast('MsgBox.msgBoxListUpdated');
        });
      },
      //获取消息盒子的Json数据
      getMsgBoxListJson:function(){
        return msgBoxListJson;
      }
    }
  });
